package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public Result order(Map map) throws Exception {
        //1.先判断当前日期是否可以进行预约
        String orderDate = (String) map.get("orderDate");

        //通过日期查询当前日期是否可以进行预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));

        //查询结果为空说明当前日期没有进行预约设置不能预约
        if (orderSetting == null)
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);

        //可预约人数等于已预约人生 预约已满
        if (orderSetting.getReservations() == orderSetting.getNumber())
            return new Result(false, MessageConstant.ORDER_FULL);


        //这里说明当前日期可以预约,则需要判断当前用户是否是会员(是否注册),如果不是会员则直接注册
        String telephone = (String) map.get("telephone");
        //这里是通过手机号代表用户
        Member member = memberDao.findTelePhone(telephone);
        String setmealId = (String) map.get("setmealId"); //套餐id
        //如果等于null 说明当前手机号尚未注册
        if (member == null) {
            //对于尚未注册的用户 直接将其注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(DateUtils.parseString2Date(orderDate));

            memberDao.add(member);
        } else {
            //对于已注册的会员判断他是否重复预约(同一个人在同一天预约了同一个套餐)
            Integer memberId = member.getId(); //会员id
            Order order = orderDao.findCondition(memberId, DateUtils.parseString2Date(orderDate), Integer.parseInt(setmealId));

            //查询结果不为空说明已经进行过预约了
            if (order != null)
                return new Result(false, MessageConstant.HAS_ORDERED);
        }

        //到这里 当前日期可预约 预约未满 会员已注册 没有进行重复预约 就可以进行预约添加了

        Integer memberId = member.getId();//会员id
        Date date = DateUtils.parseString2Date(orderDate); //预约时间
        String orderType = (String) map.get("orderType"); //预约类型
        String status = Order.ORDERSTATUS_NO; //到诊状态
        int setMealId = Integer.parseInt(setmealId); //套餐id
        Order order = new Order(memberId, date, orderType, status, setMealId);

        orderDao.add(order);

        //添加完预约之后 将当天的预约人数加1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Result findById(Integer orderId) throws Exception {

        Map map = orderDao.findById(orderId);
        Date orderDate = (Date) map.get("orderDate");

        map.put("orderDate",DateUtils.parseDate2String(orderDate));
        if (map.isEmpty())
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);

        return new Result(false,MessageConstant.QUERY_ORDER_SUCCESS,map);
    }
}
